#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "../clsv/error.h"
#include "transport/transADT.h"
#include "sessADT.h"


extern int errno;
/*--------------------------------------------------------------------------*/
int alrm = 0;
int counter= 1;

/*--------------------------------------------------------------------------*/

struct sessCDT
{
	transADT trans;	/* For especial use of the transport layer */
};


/*--------------------------------------------------------------------------*/

int initSessADT(sessADT *sess);

/*--------------------------------------------------------------------------*/

int
initSessADT(sessADT *sess)
{
	sessADT aux;

	*sess = NULL;

	if ( (aux = malloc(sizeof(struct sessCDT)) ) == NULL )
	{
		fprintf(stderr,"sessADT: Initiating sessADT, malloc error.\n");
		return ERROR_MALLOC;
	}

	*sess = aux;

	return ERROR_OK;
}

/*--------------------------------------------------------------------------*/

int
initSessServer(int type, const void *dest, sessADT *sess)
{
	int error = ERROR_OK;

	error = initSessADT(sess);
	if( error != ERROR_OK )
		return error;

	error = bindTrans(type, dest, &((*sess)->trans));

	if( error != ERROR_OK )
	{
		free(*sess);
		*sess = NULL;
		return error;
	}

	return ERROR_OK;
}

/*--------------------------------------------------------------------------*/

int
initSessClient(int type, const void *dest, sessADT *sess)
{
	int error = ERROR_OK;

	error = initSessADT(sess);
	if( error != ERROR_OK )
		return error;

	error = connTrans(type, dest, &((*sess)->trans));
	if( error != ERROR_OK )
	{
		free(*sess);
		*sess = NULL;
		return error;
	}

	return ERROR_OK;
}

/*--------------------------------------------------------------------------*/

int
listenSess(sessADT sess, sessADT *new)
{
	int error = ERROR_OK;

	error = initSessADT(new);
	if( error != ERROR_OK )
		return error;
	printf("listenSess: despues initSessADT\n");
	if(errno == EINTR)
		printf("listenSess: interrumpido por señal!!\n");
	return acceptTrans(sess->trans, &((*new)->trans));
}

/*--------------------------------------------------------------------------*/

int
closeSess(sessADT sess)
{
	int error = ERROR_OK;

	if( sess != NULL )
	{
		error = closeTrans(sess->trans);
		free(sess);
	}

	return error;
}

/*--------------------------------------------------------------------------*/

int
sendMsg(sessADT sess, void * msg, int len)
{
	return sndTrans(sess->trans, msg, len);
}

/*--------------------------------------------------------------------------*/

int
getMsg(sessADT sess, void * msg, int len, int timeO)
{
	if(sess == NULL)
		return 0;
	return rcvTrans(sess->trans, msg, len, timeO);
}

/*--------------------------------------------------------------------------*/

int
sendAns(sessADT sess, void * num, int len)
{
	return sndTrans(sess->trans, num, len);
}

/*--------------------------------------------------------------------------*/

int
getAns(sessADT sess, void * num, int len, int timeO)
{
	return rcvTrans(sess->trans, num, len, timeO);
}

/*--------------------------------------------------------------------------*/

